package com.deyuanyun.pic.settings.support.biz;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.ValveRoomDTO;
import com.deyuanyun.pic.settings.domain.pipe.PipeLine;
import com.deyuanyun.pic.settings.domain.pipe.PipeSegment;
import com.deyuanyun.pic.settings.service.pipe.PipeLineService;
import com.deyuanyun.pic.settings.service.pipe.PipeSegmentService;
import com.deyuanyun.pic.settings.service.pipe.ValveRoomService;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管道工具类
 *
 * @author huanglw
 * @date 2016-7-25
 *
 * History: [修改人][时间][修改描述]
 */
public class PipeLineUtil {
    static Logger logger= Logger.getLogger(PipeLineUtil.class);

    private static Map<String, String> pipeLineMap = new HashMap();
    private static ListMultimap<String, PipeSegment> pipeSegmentListMultimap = ArrayListMultimap.create();
    private static Map<String, String> valveRoomMap = new HashMap();

    private static final String PIPE_SEGMENT_CACHE = "APPLICATION.PIPE_SEGMENT_CACHE";

    /**
     * 得到所有的管道数据
     */
    private static void loadPipeLine(){
        PipeLineService pipeLineService = SpringAppliction.getBean(PipeLineService.class);
        List<PipeLine> list = pipeLineService.getLine();
        for (PipeLine pipeLine : list){
            pipeLineMap.put(pipeLine.getId(), pipeLine.getName());
        }
    }

    /**
     * 得到所有的场站阀室数据
     */
    private static void loadValveRoom(){
        ValveRoomService valveRoomService = SpringAppliction.getBean(ValveRoomService.class);
        List<ValveRoomDTO> list = valveRoomService.queryAllValueRooms(null);
        for (ValveRoomDTO valveRoomDTO : list){
            valveRoomMap.put(valveRoomDTO.getId(), valveRoomDTO.getName());
        }
    }

    /**
     * 得到所有的 管段数据
     */
    private static void loadPipeSegment(){

        PipeSegmentService pipeSegmentService = SpringAppliction.getBean(PipeSegmentService.class);
        List<PipeSegment> list = pipeSegmentService.getAllSegment(null);
        pipeSegmentListMultimap.clear();
        TempStoreDataSupport.removeData(PIPE_SEGMENT_CACHE);
        for (PipeSegment pipeSegment : list){
            pipeSegmentListMultimap.put(pipeSegment.getId(), pipeSegment);
        }
        TempStoreDataSupport.pushDataf(PIPE_SEGMENT_CACHE, pipeSegmentListMultimap);
    }

    /**
     * 根据id号，得到管道名称
     *
     * @param id
     * @return
     */
    public static String getPipeLineName(String id){
        loadPipeLine();
        return pipeLineMap.get(id);
    }

    /**
     * 根据id号，得到管段名称
     *
     * @param id
     * @return
     */
    public static String getPipeSegmentName(String id){

        PipeSegment pipeSegment = TempStoreDataSupport.pullData(PIPE_SEGMENT_CACHE+"_"+id);
        if (pipeSegment != null){
            return pipeSegment.getName();
        }
        loadPipeSegment();
        List<PipeSegment> pipeSegmentList = TempStoreDataSupport.pullData(PIPE_SEGMENT_CACHE);
        if (ObjectUtil.isEmptyAll(pipeSegmentList)){
            pipeSegmentList = new ArrayList<PipeSegment>(pipeSegmentListMultimap.values());
            TempStoreDataSupport.pushDataByIdelTimeMinute(PIPE_SEGMENT_CACHE, pipeSegmentListMultimap, 5);
        }
        for (PipeSegment pipeSegment1 : pipeSegmentList){
            if (StringUtils.isNotNullEmpty(id) && id.equals(pipeSegment1.getId())){
                TempStoreDataSupport.pushDataByIdelTimeMinute(pipeSegment1+"_"+id, pipeSegment1, 2);
                return pipeSegment1.getName();
            }
        }
        return null;
    }

    /**
     * 根据id号，得到对应场站阀室名称
     *
     * @param id
     * @return
     */
    public static String getValveRoomName(String id){
        loadValveRoom();
        return valveRoomMap.get(id);
    }

}
